package net.big_oh.postoffice;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.Set;

import net.big_oh.common.utils.PropertyUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/*
 Copyright (c) 2010 Dave Wingate dba Big-Oh Software (www.big-oh.net)

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * Provides static factory methods for constructing concrete instances of
 * {@link IPostOfficeService} using {@link Properties} files/objects to define
 * service attributes.
 * 
 * @see IPostOfficeService
 * @author dwingate
 * @version Jun 1, 2010
 */
public class PostOfficeServiceFactory
{

	private static final Log logger = LogFactory.getLog(PostOfficeServiceFactory.class);

	/**
	 * The name of the properties file that, by default, will be used to build
	 * new PostOffice instances.
	 */
	public static final String PROPERTIES_FILE_NAME = "postoffice.properties";

	/**
	 * The hostname of the SMTP server that will be used to route out-bound
	 * email messages.
	 */
	public static final String PROPERTY_KEY_HOST = "postoffice.host";

	/**
	 * The port on which the outgoing SMTP server is listening for connections.
	 */
	public static final String PROPERTY_KEY_PORT = "postoffice.port";

	/**
	 * The "from" address for all email messages sent from the PostOffice
	 * instance.
	 */
	public static final String PROPERTY_KEY_FROM = "postoffice.from";

	/**
	 * Enable to log debug output generated by the JavaMail API.
	 */
	public static final String PROPERTY_KEY_DEBUG = "postoffice.debug";

	/**
	 * By associating a value of 'true' with this key, you can request that
	 * email messages be transported using the secure SSL protocol.
	 */
	public static final String PROPERTY_KEY_TRANSPORT_WITH_SSL = "postoffice.transportWithSsl";

	/**
	 * By associating a value of 'true' with this key, you can request that the
	 * secure TLS protocol be used when authenticating to the outgoing SMTP mail
	 * server.
	 */
	public static final String PROPERTY_KEY_AUTHENTICATE_WITH_TLS = "postoffice.authenticateWithTls";

	/**
	 * The username for SMTP authentication. If you supply a username, you
	 * should also supply a password.
	 * 
	 * @see #PROPERTY_KEY_SMTP_PASSWORD
	 */
	public static final String PROPERTY_KEY_SMTP_USER_NAME = "postoffice.smtpUserName";

	/**
	 * The password for SMTP authentication. If you supply a password, you
	 * should also supply a username.
	 * 
	 * @see #PROPERTY_KEY_SMTP_USER_NAME
	 */
	public static final String PROPERTY_KEY_SMTP_PASSWORD = "postoffice.smtpPassword";

	// Define & expose a set of required property keys
	static final String[] mandatoryPropertyKeys = { PROPERTY_KEY_HOST, PROPERTY_KEY_FROM };
	/**
	 * The property keys included in this collection define the minimum set of
	 * keys that must be passed to the {@link #getInstance()} and
	 * {@link #getInstance(Properties)} methods.
	 */
	public static final Set<String> MANDATORY_PROPERTY_KEYS = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(mandatoryPropertyKeys)));

	/**
	 * A convenience wrapper around {@link #getInstance(Properties)}. Calling
	 * this method is equivalent to:
	 * <code>PostOfficeServiceFactory.getInstance(PostOfficeServiceFactory.readDefaultPostOfficeProperties())</code>
	 * 
	 * @return an instance of {@link IPostOfficeService} constructed from the
	 *         properties file defined in the {@link #PROPERTIES_FILE_NAME}
	 *         file.
	 * 
	 * @throws NullPointerException
	 *             Thrown if no properties file named
	 *             {@link #PROPERTIES_FILE_NAME} can be resolved from the class
	 *             path.
	 * @throws IllegalArgumentException
	 *             Thrown if the properties file at
	 *             {@link #PROPERTIES_FILE_NAME} does not contain all mandatory
	 *             property keys as defined in {@link #MANDATORY_PROPERTY_KEYS}.
	 * 
	 * @see #PROPERTIES_FILE_NAME
	 * @see #MANDATORY_PROPERTY_KEYS
	 */
	public static IPostOfficeService getInstance()
	{
		return getInstance(readDefaultPostOfficeProperties());
	}

	/**
	 * @param postOfficeProperties
	 *            The properties to be used in constructing the new PostOffice
	 *            instance.
	 * @return an instance of {@link IPostOfficeService} constructed from the
	 *         details provided in the postOfficeProperties parameter.
	 * @throws NullPointerException
	 *             Thrown if the postOfficeProperties argument is null.
	 * @throws IllegalArgumentException
	 *             Thrown if postOfficeProperties does not contain all mandatory
	 *             property keys as defined in {@link #MANDATORY_PROPERTY_KEYS}.
	 * 
	 * @see #MANDATORY_PROPERTY_KEYS
	 */
	public static IPostOfficeService getInstance(Properties postOfficeProperties)
	{
		// validate post office properties
		if (postOfficeProperties == null)
		{
			throw new NullPointerException("postOfficeProperties file cannot be null");
		}

		for (String mandatoryKey : MANDATORY_PROPERTY_KEYS)
		{
			if (!postOfficeProperties.containsKey(mandatoryKey))
			{
				throw new IllegalArgumentException("Properties file '" + PROPERTIES_FILE_NAME + "' does not contain the required key '" + mandatoryKey + "'");
			}
		}

		final String smtpServerHost = postOfficeProperties.getProperty(PROPERTY_KEY_HOST);
		final String sendersFromAddress = postOfficeProperties.getProperty(PROPERTY_KEY_FROM);
		final PostOfficeService.Builder builder = new PostOfficeService.Builder(smtpServerHost, sendersFromAddress);

		builder.setSmtpServerPort(Integer.valueOf(postOfficeProperties.getProperty(PROPERTY_KEY_PORT, Integer.toString(0))));
		builder.setDebug(Boolean.valueOf(postOfficeProperties.getProperty(PROPERTY_KEY_DEBUG, Boolean.toString(false))));
		builder.setTransportWithSsl(Boolean.valueOf(postOfficeProperties.getProperty(PROPERTY_KEY_TRANSPORT_WITH_SSL, Boolean.toString(false))));
		builder.setAuthenticateWithTls(Boolean.valueOf(postOfficeProperties.getProperty(PROPERTY_KEY_AUTHENTICATE_WITH_TLS, Boolean.toString(false))));
		builder.setSmtpUserName(postOfficeProperties.getProperty(PROPERTY_KEY_SMTP_USER_NAME));
		builder.setSmtpPassword(postOfficeProperties.getProperty(PROPERTY_KEY_SMTP_PASSWORD));

		return builder.build();
	}

	/**
	 * 
	 * @return a Properties object populated from the properties file located at
	 *         {@link #PROPERTIES_FILE_NAME}.
	 */
	public static Properties readDefaultPostOfficeProperties()
	{
		Properties props = null;

		try
		{
			props = PropertyUtil.loadProperties(PROPERTIES_FILE_NAME);
		}
		catch (MissingResourceException mre)
		{
			logger.error(mre);
		}

		if (props == null)
		{
			logger.error("PostOffice is not properly configured.  Please add properties file '" + PROPERTIES_FILE_NAME + "' to the classpath.");
		}

		return props;
	}

}
